#include "MsgTipsLabel.h"
#include<QPainter>
MsgTipsLabel::MsgTipsLabel(QString content, QWidget *parent)
	: QWidget(parent)
{
	alpha = 255;
	tips = content;
	//this->rect = rect;
	connect(&paintTimer, &QTimer::timeout, [=] {alpha--; if (alpha < 5) { paintTimer.stop(); this->deleteLater(); return; }this->update(); });
	paintTimer.start(20);
}

MsgTipsLabel::~MsgTipsLabel()
{
}

void MsgTipsLabel::startCloseCountDown(uint seconds)
{
	QTimer::singleShot(seconds * 1000, [=]() {this->deleteLater(); });
}

void MsgTipsLabel::paintEvent(QPaintEvent* paintEvent)
{
	QPainter painter(this);
	QFont texFont;
	texFont.setPixelSize(15);
	painter.setFont(texFont);
	painter.setPen(QColor(255, 255, 255));
	QRect paintRect = QRect(0, 0, size().width(), size().height());
	painter.setBrush(QColor(244, 84, 84,alpha));
	painter.drawRoundedRect(paintRect, 16,16);

	QRect iconRect(10,5,20,20);
	painter.drawPixmap(iconRect, QPixmap(":/img/stylesheet/images/alarm.png").scaled(QSize(20, 20)));
	painter.setPen(QColor(255, 255, 255));
	QTextOption textOpt;
	textOpt.setAlignment(Qt::AlignCenter);
	painter.drawText(paintRect, tips, textOpt);
}

void MsgTipsLabel::mousePressEvent(QMouseEvent* event)
{
	paintTimer.stop();
	deleteLater();
}

